home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.05 May 90 / PrintWindow Code / PrintWD.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-02  |  8.3 KB  |  313 lines  |  [TEXT/KAHL]

  1. /*******************
  2. PrintWD.c
  3. *******************/
  4.  
  5. /**********************
  6. Include files
  7. ***********************/
  8. #include "String.h"
  9. #include "Messenger.h"
  10. #include "PrintTraps.h"
  11.  
  12. /**********************
  13. Structures
  14. ***********************/
  15. typedef struct ditlheader {
  16.     long HorP;
  17.     Rect displayRect;
  18.     char itemType;
  19.     char dataLength;
  20.     } ditlheader, *ditlheaderPtr, **ditlheaderHdl;
  21.  
  22. /**********************
  23. Globals
  24. ***********************/
  25. THPrint    ThePrintRec; /* Printing Stuff */
  26. TPPrPort    ThePrintPort;
  27. TPrStatus    PrintStatus;
  28. Rect    PageRect;
  29. char    forPrinting;
  30.  
  31. /*************************************************/
  32. /*********** Printing Routines *******************/
  33. /*************************************************/
  34.  
  35. /***************************/
  36. /* PrintControls() handles printing of standard buttons */
  37. PrintControls(theControl)
  38. ControlHandle theControl;
  39. { /* PrintControls() */
  40. ProcPtr controlRoutine;
  41. long dummy;
  42. int varCode, hilite;
  43.  
  44. while (theControl != NULL) { /* control loop */
  45.     varCode = GetCVariant(theControl);
  46.     hilite = (**theControl).contrlHilite;
  47.     
  48.     HiliteControl(theControl, 0); /* LW does not support XOR */
  49.     HLock((Handle) (**theControl).contrlDefProc);
  50.     controlRoutine =(ProcPtr) *((**theControl).contrlDefProc);
  51.     dummy = CallPascalL(varCode, theControl, 0, 0L, controlRoutine); /* Call CDEF to Draw */
  52.     HUnlock((Handle) (**theControl).contrlDefProc);
  53.     HiliteControl(theControl, hilite);
  54.     
  55.     theControl = (**theControl).nextControl;
  56.     } /* control loop */
  57. } /* PrintControls() */
  58.  
  59. /***************************/
  60. /* PrintList() handles printing of standard list */
  61. PrintList(theList)
  62. ListHandle theList;
  63. { /* PrintList() */
  64. Cell lCell;
  65. Rect lRect, dstRect;
  66. int lDataOffset, lDataLen;
  67. char dataPtr[255];
  68.  
  69. if (theList == NULL) return;
  70. lCell.h =  0;
  71. lCell.v = 0;
  72. LDoDraw(TRUE, theList);
  73. do { /* loop through cells */
  74.     LFind(&lDataOffset, &lDataLen, lCell, theList);
  75.     LGetCell(&dataPtr, &lDataLen, lCell, theList);
  76.     /*LFind(&lDataOffset, &lDataLen, lCell, theList);
  77.     LSetCell(&dataPtr, lDataLen, lCell, pList);
  78.     LDraw(lCell, pList);*/
  79.     LRect(&lRect, lCell, theList);
  80.     if (SectRect(&((**theList).rView), &lRect, &dstRect))
  81.         if (EqualRect(&lRect, &dstRect)) { /* Draw it */
  82.             TextBox(&dataPtr, lDataLen, &lRect, teJustLeft);
  83.             }
  84.     } while (LNextCell(TRUE, TRUE, &lCell, theList)); /* loop through cells */
  85. } /* PrintList() */
  86.  
  87. /***************************/
  88. /* StrReplace() substitutes the first occurrence of t with r in s
  89.     It returns TRUE if there was a substitution*/
  90. int StrReplace(s, t, r)
  91. char *s, *t, *r;
  92. { /* StrReplace() */
  93. char temp[256], *p;
  94.  
  95. strcpy(temp, s);
  96. p = strstr(temp, t); /* find first occurrence */
  97. if (*p == '\0') return (0); /* no occurence */
  98.  
  99. *p = '\0'; /* delete t */
  100. p = p + strlen(t); /* go to second half of string */
  101.  
  102. strcpy(s, temp); /* get first part */
  103. strcat(s, r); /* put in replacement */
  104. strcat(s, p); /* put in second half */
  105.  
  106. return (1);
  107. } /* StrReplace() */
  108.  
  109. /***************************/
  110. /* PrintDialog() prints dialog substituting sX for ParamText */    
  111. PrintDialog(d, s0, s1, s2, s3)
  112. DialogPeek d;
  113. Str255 s0, s1, s2, s3;
  114. { /* PrintDialog() */
  115. ditlheader *dh;
  116. Ptr p;
  117. int itemCount, i, theItem, datalen;
  118. int ResID, theType;
  119. Handle hItem;
  120. char text[256];
  121. Rect tempRect, box;
  122.  
  123. p = (Ptr) *(*d).items;
  124. itemCount = *((int *) p); /* get number of dialog items */
  125.  
  126. p = p + 2;
  127. for (i=0; i<= itemCount; i++) { /* Item Loop */
  128.     dh = (ditlheader *) p;
  129.     
  130.     /* get type */
  131.     theItem = dh->itemType;
  132.     if (theItem < 0)
  133.         theItem = theItem * -1;
  134.     if (theItem >= 128)
  135.         theItem -= 128;
  136.     
  137.     /* get length of data */
  138.     datalen = dh->dataLength;
  139.     if ((datalen % 2) != 0)
  140.         datalen += 1;
  141.         
  142.     switch (theItem) { /* ItemType Switch */
  143.         case ctrlItem + btnCtrl: /* Standard Button */
  144.         case ctrlItem + chkCtrl: /* Checkbox */
  145.         case ctrlItem + radCtrl: /* Radio Button */
  146.         case ctrlItem + resCtrl: /* Resource Control */
  147.             PrintControls((ControlHandle) dh->HorP); /* Draw controls */
  148.             break;
  149.         
  150.         case editText:
  151.         case statText:
  152.             GetDItem((DialogPtr) d, i + 1, &theType, &hItem, &box);
  153.             GetIText(hItem, (Str255 *) text);
  154.             PtoCstr(text);
  155.             
  156.             /* make substitution of ParamText */
  157.             PtoCstr((char *) s0);
  158.             PtoCstr((char *) s1);
  159.             PtoCstr((char *) s2);
  160.             PtoCstr((char *) s3);
  161.             while(StrReplace(text, "^0", (char *) s0));
  162.             while(StrReplace(text, "^1", (char *) s1));
  163.             while(StrReplace(text, "^2", (char *) s2));
  164.             while(StrReplace(text, "^3", (char *) s3));
  165.             CtoPstr((char *) s0);
  166.             CtoPstr((char *) s1);
  167.             CtoPstr((char *) s2);
  168.             CtoPstr((char *) s3);
  169.             
  170.             TextBox(text, strlen(text), &(dh->displayRect), teJustLeft); /* draw text */
  171.             if (theItem == editText) { /* draw edit text box */
  172.                 tempRect = dh->displayRect;
  173.                 FrameRect(&tempRect);
  174.                 } 
  175.             break;
  176.             
  177.         case iconItem:
  178.             PlotIcon(&(dh->displayRect), (Handle) dh->HorP); /* draw icon */
  179.             break;
  180.             
  181.         case picItem:
  182.             DrawPicture((PicHandle) dh->HorP, &(dh->displayRect)); /* draw picture */
  183.             break;
  184.             
  185.         case userItem:
  186.             CallPascal((WindowPtr) ThePrintPort, i+ 1, (ProcPtr) dh->HorP); /* draw user item */
  187.             break;
  188.             
  189.         default:
  190.             break;
  191.         } /* ItemType Switch */
  192.     p = p + sizeof(ditlheader) + datalen;
  193.     } /* Item Loop */
  194. } /* PrintDialog() */
  195.  
  196. /***************************/
  197. /* InitPrint() initializes print variables */
  198. InitPrint()
  199. {
  200. ThePrintRec = NUL;
  201. PrOpen();
  202. ThePrintRec = (THPrint) NewHandle(sizeof(TPrint));
  203. PrintDefault(ThePrintRec);
  204. PageRect = (*ThePrintRec)->prInfo.rPage;
  205. PrClose();
  206. forPrinting = FALSE;
  207. PrSetError(noErr);
  208. } /* end InitPrint() */
  209.  
  210. /***************************/
  211. /* doPageSetUp() handles page setup dialog */
  212. doPageSetUp()
  213. {
  214. char    confirmed;
  215. WindowPtr    SavePort;
  216. OSErr theError;
  217.  
  218. InitCursor();
  219. GetPort(&SavePort);
  220. PrOpen();
  221. confirmed = PrValidate(ThePrintRec);
  222. confirmed = PrStlDialog(ThePrintRec);
  223. PrClose();
  224. theError = PrError();
  225. SetPort(SavePort);
  226. if (confirmed) {
  227.     PageRect = (*ThePrintRec)->prInfo.rPage;
  228.     }
  229. } /* end doPageSetUp() */
  230.  
  231. /***************************/
  232. /* PrintWindow() handles printing */
  233. PrintWindow(theWindow)
  234. WindowPtr theWindow;
  235. {
  236. char    DoIt; /* flag to do printing */
  237. TPrStatus    PrintStatus; /* print variables */
  238. TPPrPort    myPrPort;
  239. WindowPtr    savePort;
  240. int    copies, i, j, firstPage, lastPage, oldiPlayer;
  241. char    dummy;
  242. Rect    tempRect, pictRect, nameRect, windowRect;
  243. OSErr theError;
  244. GrafPtr aPort;
  245. Point oldOrigin;
  246.  
  247. GetPort(&savePort); /* initialize printing */
  248. InitCursor();
  249. PrOpen();
  250.  
  251. if (PrError() == noErr){ /* do print dialog */
  252.     DoIt = PrValidate(ThePrintRec);
  253.     DoIt = PrJobDialog(ThePrintRec);
  254.     dummy = (int) AnOSError(PrError(), "\pProblem with Print Dialog", "\p");
  255.     
  256.     if (DoIt && !dummy) { /* Print Document */
  257.         tempRect = theWindow->portRect;
  258.         /* SetCursor(*watch); */
  259.         InsetRect(&tempRect, -4, -4);
  260.         pictRect = tempRect; /* set print variables up */
  261.         PositionRect(&pictRect, &PageRect, CENTER, THIRD);
  262.         
  263.         ThePrintPort = PrOpenDoc(ThePrintRec, NUL, NUL);
  264.         theError = PrError();
  265.         if (!AnOSError(theError, "\pProblem with Printer", "\p")) { /* good port */
  266.             /* get copies and page range */
  267.             copies = (*ThePrintRec)->prJob.iCopies;
  268.             firstPage = (*ThePrintRec)->prJob.iFstPage;
  269.             lastPage = (*ThePrintRec)->prJob.iLstPage;
  270.  
  271.             if ((*ThePrintRec)->prJob.bJDocLoop == bSpoolLoop) /* check for spooling */
  272.                 copies = 1;
  273.                 
  274.             /* check bad page range */
  275.             if (firstPage > lastPage) {
  276.                 InitCursor();
  277.                 dummy = Message(M_OK, noIcon, "\pBad page range", "\p", "\p", "\p");
  278.                 }
  279.             else { /* good page range */
  280.                 for (i=0; i<copies; i++) { /* valid picture */
  281.                     PrOpenPage(ThePrintPort, NUL); /* print page */
  282.                     theError = PrError();
  283.                     if (!AnOSError(theError, "\pProblem with page", "\p")) {
  284.                         FrameRect(&pictRect);
  285.                         GetPort(&aPort);
  286.                         oldOrigin = topLeft(aPort->portRect);
  287.                         SetOrigin(oldOrigin.h - pictRect.left, oldOrigin.v - pictRect.top);
  288.                         forPrinting = TRUE;
  289.                         
  290.                         UpDate_MyWindow(theWindow);
  291.                         UpDate_AboutDialog(theWindow);
  292.                         UpDate_TestDialog(theWindow);
  293.                         
  294.                         forPrinting = FALSE;
  295.                         SetOrigin(oldOrigin.h, oldOrigin.v);                            
  296.                         }
  297.                     PrClosePage(ThePrintPort);
  298.                     } /*  end valid picture */
  299.                 }
  300.             } /* end else good port */
  301.         PrCloseDoc(ThePrintPort);
  302.         if (((*ThePrintRec)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
  303.             PrPicFile(ThePrintRec, NUL, NUL, NUL, &PrintStatus); 
  304.             /* print spool file, if any */
  305.             
  306.         } /* end of print document */
  307.     } /* no error on PrOpen() */
  308.  
  309. PrClose();
  310. SetPort(savePort);
  311. InvalRect(&tempRect);
  312. InitCursor();
  313. } /* end doPrint() */